<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>加减案例</title>
  <script src="../vue.js"></script>
</head>

<body>
  <div id="app">
    <ul>
      <li v-for="(item,index) in arr" :key="item.id">
        {{item.content}}
        <button @click="del(item.id)">删除</button>
      </li>
    </ul>
    <button @click="add">添加</button>
  </div>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        arr: [
          // { id: 1, content: '张三1' },
          // { id: 2, content: '张三2' },
          // { id: 3, content: '张三3' },
          // { id: 4, content: '张三4' },
        ]
      },
      methods: {
        del(id) {
          this.arr = this.arr.filter(item => item.id !== id)
        },
        add() {
          let random = Math.floor(Math.random() * 10)
          console.log(random);
          this.arr.push(
            {
              id: +new Date(),
              content: random
            })
        }
      }
    })
  </script>
</body>

</html>